﻿using System;
using System.Collections.Generic;
using CzfSDK;
using MySql.Data.MySqlClient;
using System.Data;
using System.Collections;

namespace Server
{
	public static class SkillManager
	{
		public static List<int> GetSkillIds(int userId)
		{
			return GetAll (userId);
		}

		public static List<int> GetSkillShortIds(int userId)
		{
			var list = GetAll (userId);
			var ret = new List<int> ();
			if (list != null) {
				foreach (var skillId in list) {
					var config = MConfig.GetSkillTemplate()[skillId];
					ret.Add (config.GetVal<int>("id"));
				}
			}
			return ret;
		}

		// 碎片兑换技能
		public static string AddSkillByDust(int userId, int skillId)
		{
			var config = MConfig.GetSkillConfig (skillId);
			if(config == null)
				throw new CzfProtoException (Constants.ERR3, "不存在的技能");

			var has = Get (userId, skillId);
			if (has != null) {
				throw new CzfProtoException (Constants.ERR3, "已经有这个技能了");
			}

			var quality = config.GetVal<int> ("quality");
			int count;
			if (quality == 4)
				count = MConfig.GetGameConfig<int>("skillcost4");
			else if (quality == 3)
				count = MConfig.GetGameConfig<int>("skillcost3");
			else if (quality == 2)
				count = MConfig.GetGameConfig<int>("skillcost2");
			else
				count = MConfig.GetGameConfig<int>("skillcost1");

			// 碎片
			BagManager.DelItem(BagManager.DUST_ID + "," + count, userId, "碎片兑换技能", skillId.ToString());

			// 发奖励
			BagManager.AddItem(skillId + ",1", userId, "碎片兑换技能", skillId.ToString());

			return "ok";
			
		}

		#region 给玩家增加技能
		public static void TryAddSkill(int userId, int skillId)
		{
			if (!MConfig.GetSkillTemplate().ContainsKey(skillId))
				throw new CzfProtoException (Constants.ERR3, "不存在的技能ID:" + skillId);
		}

		// 给玩家增加技能
		public static string AddSkill(int userId, int skillId)
		{
			var has = Get (userId, skillId);
			if (has != null) {
				// 已经有技能了,转化为别的资源
				string prize = DustVal(skillId);
				BagManager.AddItem(prize, userId, "多余技能转化", skillId.ToString());
				return prize;
			}

			Create (userId, skillId);

			return skillId + ",1";
		}

		// 技能转换为碎片数量
		static string DustVal(int skillId)
		{
			var skillConfigs = MConfig.GetSkillTemplate ();
			var skillConfig = skillConfigs [skillId];
			var quality = skillConfig.GetVal<int> ("quality");

			int count = 5;
			if (quality == 4)
				count = MConfig.GetGameConfig<int>("skilladd4");
			else if (quality == 3)
				count = MConfig.GetGameConfig<int>("skilladd3");
			else if (quality == 2)
				count = MConfig.GetGameConfig<int>("skilladd2");
			else
				count = MConfig.GetGameConfig<int>("skilladd1");

			return BagManager.DUST_ID + "," + count;
		}
		#endregion

		#region DB
		// 技能胜利次数统计
		public static void SkillWin(int skillId)
		{
			var sql = "INSERT INTO skilllog (id, wincount, loscount) VALUES (@id,1,0) ON DUPLICATE KEY UPDATE wincount=wincount+1; ";
			MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@id", skillId)
			);
		}

		// 技能失败次数统计
		public static void SkillLose(int skillId)
		{
			var sql = "INSERT INTO skilllog (id, wincount, loscount) VALUES (@id,0,1) ON DUPLICATE KEY UPDATE loscount=loscount+1; ";
			MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@id", skillId)
			);
		}

		// 查询玩家的某个技能
		public static Hashtable Get(int userId, int skillId)
		{
			var sql = "select * from skill where userid = @userid and skillid = @skillid";
			var dr = MysqlPool.current.ExecDataRow (sql,
				new MySqlParameter ("@userid", userId),
				new MySqlParameter ("@skillid", skillId)
			);
			if (dr == null)
				return null;
			else
				return dr.ToHashtable ();
		}

		// 查询某玩家的技能列表
		public static List<int> GetAll(int userId)
		{
			var sql = "select * from skill where userid = @userid";
			var dt = MysqlPool.current.ExecDataTable (sql,
				new MySqlParameter("@userid", userId)
			);
			var ret = new List<int> ();
			if (dt != null) {
				foreach (DataRow dr in dt.Rows) {
					ret.Add (Convert.ToInt32 (dr ["skillid"]));
				}
			}
			return ret;
		}

		// 给玩家增加一个技能
		private static int Create(int userId, int skillId)
		{
			var sql = "insert into skill(userid, skillid, createtime) values(@userid, @skillid, now())";
			return MysqlPool.current.ExecuteNonQuery (sql, 
				new MySqlParameter ("@userid", userId),
				new MySqlParameter ("@skillid", skillId)
			);
		}
		#endregion
	}
}

